package com.licheedev.adplayer.loader;

import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.licheedev.adplayer.R;
import com.licheedev.adplayer.data.AdData;
import com.licheedev.adplayer.data.AdDataHelper;
import java.net.URI;

/**
 * 默认的视频下载器
 */
public class DefaultGlideImageLoader implements ImageLoader {

    protected static final RequestOptions DEFAULT_REQUEST_OPTS =
        new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate()
            .placeholder(R.drawable.ic_default_ad_placeholder);

    private Callback mCallback;

    @Override
    public void loadImage(@NonNull ImageView imageView, @NonNull AdData adData,
        @NonNull Callback callback) {
        URI uri = adData.getURI();
        RequestBuilder<Drawable> requestManager;
        @AdData.Scheme String scheme = uri.getScheme().toLowerCase();
        switch (scheme) {
            case AdData.SCHEME_HTTP:
            case AdData.SCHEME_HTTPS:
                requestManager = Glide.with(imageView).load(AdDataHelper.toUrl(uri));
                break;
            case AdData.SCHEME_FILE:
                requestManager = Glide.with(imageView).load(AdDataHelper.toFile(uri));
                break;
            case AdData.SCHEME_DRAWABLE:
                requestManager = Glide.with(imageView).load(AdDataHelper.toDrawableId(uri));
                break;
            case AdData.SCHEME_RAW: {
                requestManager = Glide.with(imageView)
                    .load(AdDataHelper.toRawForGlide(uri, imageView.getContext()));
                break;
            }
            case AdData.SCHEME_ASSET: {
                requestManager = Glide.with(imageView).load(AdDataHelper.toAssetForGlide(uri));
                break;
            }
            default:
                callback.onFailure(
                    new IllegalArgumentException("Not support loading uri:{" + uri + "}"));
                return;
        }

        this.mCallback = callback;

        toConfigRequest(requestManager).listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model,
                Target<Drawable> target, boolean isFirstResource) {
                mCallback.onFailure(new RuntimeException("加载图片失败"));
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target,
                DataSource dataSource, boolean isFirstResource) {
                mCallback.onSuccess();
                return false;
            }
        }).into(imageView);
    }

    /**
     * 自定义加载图片配置
     *
     * @param requestBuilder
     * @return
     */
    @NonNull
    protected RequestBuilder<Drawable> toConfigRequest(
        @NonNull RequestBuilder<Drawable> requestBuilder) {
        return requestBuilder.apply(DEFAULT_REQUEST_OPTS);
    }
}
